{% extends "base.html" %}
{% block content %}
<div class="container mt-4">
    <div class="row">
        <!-- 添加储蓄目标表单 -->
        <div class="col-md-4">
            <div class="card mb-4">
                <div class="card-body">
                    <h5 class="card-title">添加储蓄目标</h5>
                    <form action="{{ url_for('add_savings_goal') }}" method="POST">
                        <div class="mb-3">
                            <label for="name" class="form-label">目标名称</label>
                            <input type="text" class="form-control" id="name" name="name" required>
                        </div>
                        <div class="mb-3">
                            <label for="target_amount" class="form-label">目标金额</label>
                            <input type="number" step="0.01" class="form-control" id="target_amount" name="target_amount" required>
                        </div>
                        <div class="mb-3">
                            <label for="target_date" class="form-label">目标日期</label>
                            <input type="date" class="form-control" id="target_date" name="target_date" required>
                        </div>
                        <button type="submit" class="btn btn-primary">添加目标</button>
                    </form>
                </div>
            </div>
        </div>

        <!-- 储蓄目标列表 -->
        <div class="col-md-8">
            <div class="card">
                <div class="card-body">
                    <h5 class="card-title text-center">我的储蓄目标</h5>
                    {% if goals %}
                        <div class="row">
                            {% for goal in goals %}
                            <div class="col-md-6 mb-4">
                                <div class="card">
                                    <div class="card-body">
                                        <h6 class="card-title">{{ goal.name }}</h6>
                                        <p class="card-text">
                                            目标金额: ¥{{ "%.2f"|format(goal.target_amount) }}<br>
                                            已存金额: ¥{{ "%.2f"|format(goal.current_amount) }}<br>
                                            目标日期: {{ goal.target_date.strftime('%Y-%m-%d') }}
                                        </p>
                                        <div class="progress">
                                            <div class="progress-bar" 
                                                 role="progressbar" 
                                                 style="width: {{ (goal.current_amount / goal.target_amount * 100)|round(2) }}%">
                                            </div>
                                        </div>
                                        <div class="mt-3">
                                            <button class="btn btn-primary btn-sm me-2" onclick="showDepositModal({{ goal.id }}, '{{ goal.name }}')">
                                                存入
                                            </button>
                                            <button class="btn btn-outline-primary btn-sm" onclick="showWithdrawModal({{ goal.id }}, '{{ goal.name }}')">
                                                取出
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            {% endfor %}
                        </div>
                    {% else %}
                        <p class="text-muted">还没有设置任何储蓄目标</p>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 存入弹窗 -->
<div class="modal fade" id="depositModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">存入金额到储蓄目标</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <form id="depositForm">
                    <input type="hidden" id="depositGoalId">
                    <div class="mb-3">
                        <label class="form-label">目标名称</label>
                        <input type="text" class="form-control" id="depositGoalName" readonly>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">存入金额</label>
                        <input type="number" step="0.01" class="form-control" id="depositAmount" required>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="depositToGoal()">确认存入</button>
            </div>
        </div>
    </div>
</div>
<!-- 取出弹窗 -->
<div class="modal fade" id="withdrawModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">从储蓄目标取出</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <form id="withdrawForm">
                    <input type="hidden" id="withdrawGoalId">
                    <div class="mb-3">
                        <label class="form-label">目标名称</label>
                        <input type="text" class="form-control" id="withdrawGoalName" readonly>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">可取出金额</label>
                        <input type="text" class="form-control" id="withdrawAvailable" readonly>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">取出金额</label>
                        <input type="number" step="0.01" class="form-control" id="withdrawAmount" required>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="withdrawFromGoal()">确认取出</button>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
function showDepositModal(goalId, goalName) {
    document.getElementById('depositGoalId').value = goalId;
    document.getElementById('depositGoalName').value = goalName;
    document.getElementById('depositAmount').value = '';
    new bootstrap.Modal(document.getElementById('depositModal')).show();
}

function depositToGoal() {
    const goalId = document.getElementById('depositGoalId').value;
    const amount = document.getElementById('depositAmount').value;
    fetch('/deposit_to_goal', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        body: `goal_id=${goalId}&amount=${amount}`
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            location.reload();
        } else {
            alert(data.message);
        }
    });
}

function showWithdrawModal(goalId, goalName) {
    document.getElementById('withdrawGoalId').value = goalId;
    document.getElementById('withdrawGoalName').value = goalName;
    fetch(`/api/savings_goal/${goalId}`)
        .then(response => response.json())
        .then(data => {
            document.getElementById('withdrawAvailable').value = data.current_amount;
            document.getElementById('withdrawAmount').value = '';
            new bootstrap.Modal(document.getElementById('withdrawModal')).show();
        });
}

function withdrawFromGoal() {
    const goalId = document.getElementById('withdrawGoalId').value;
    const amount = document.getElementById('withdrawAmount').value;
    fetch('/withdraw_from_goal', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        body: `goal_id=${goalId}&amount=${amount}`
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            location.reload();
        } else {
            alert(data.message);
        }
    });
}
</script>
{% endblock %}